package com.boxuegu.dao;

import com.boxuegu.controller.CardController;
import com.boxuegu.domain.Card;
import java.io.*;
import java.util.ArrayList;
public class CardDao {

    private static ArrayList<Card> cards = new ArrayList<>();

    //将集合中的数据添加到本地存储中
    public void addUserFile() throws IOException {
        //创建file对象，进行关联
        File userFile = new File("F:\\bm\\beijingmetro\\src\\card.txt");
        BufferedWriter bwUserFile = new BufferedWriter(new FileWriter(userFile));

        //将集合中数据写入到硬盘中
        for (Card card : cards) {
            bwUserFile.write(String.valueOf(card));
            bwUserFile.newLine();
            bwUserFile.flush();
        }
        bwUserFile.close();

    }

    //本地读取到集合
    private  void readUserFile() throws IOException {
        //创建file对象，进行关联
        File userFile = new File("F:\\bm\\beijingmetro\\src\\card.txt");
        BufferedReader brUserFile = new BufferedReader(new FileReader(userFile));

        //本地写到集合

        String line;
        while ((line = brUserFile.readLine()) != null) {
            String[] str = line.split("\t");
            String[] splitName = str[1].split(":");
            String[] splitId = str[2].split(":");
            String[] splitCardBalance = str[3].split(":");
            //将分割的字符串存储到card对象里
            Card card = new Card();
            card.setUserName(splitName[1]);
            card.setUserId(splitId[1]);//ArrayIndexOutOfBoundsException
            card.setCardBalance(Double.parseDouble(splitCardBalance[1]));
            //将封装好的数据添加到集合中
            cards.add(card);
        }
        //关流
        brUserFile.close();
    }

    //添加用户
    public boolean addUser(Card card) throws IOException {
        cards.add(card);//添加用户
        addUserFile();//存储到硬盘
        return true;//返回结果
    }

    //查询所有用户
    public Card[] findAllCard() throws IOException {
//        Card[] card = new Card[cards.size()];
//        for (int i = 0; i < card.length; i++) {
//            card[i] = cards.get(i);
//        }
//        return card;
 //       readUserFile();
        Card[] card = new Card[cards.size()];
        for (int i = 0; i < card.length; i++) {
            card[i] = cards.get(i);
        }
        return card;

    }

    //删除用户
    public void deleteCardById(String delId) throws IOException {
        //根据id获得索引
        int index = getIndex(delId);
        //然后进行删除
        cards.remove(index);

        addUserFile();
    }

    //查找id在容器中的索引位置
    private int getIndex(String delId) {
        //假设id不存在,索引为-1
        int index = -1;
        //遍历获取每一个地铁用户
        for (int i = 0; i < cards.size(); i++) {
            Card card = cards.get(i);
            //判断该用户不是空并且和要删除的id卡号一致
            if (card != null && card.getUserId().equals(delId)) {
                //将该索引位置赋值给index
                index = i;
                break;
            }

        }
        return index;
    }


    //查询余额的方法
    public double cardBalance(String recId) {
        int index = getIndex(recId);
        Card card = cards.get(index);
        return card.getCardBalance();
    }

    public void cutPayment(String id, int count) throws IOException {
        int i = getIndex(id);
        Card c = cards.get(i);
        if (c.getCardBalance() < count) {
            System.out.println("您的余额不足，请先充值");
            CardController cardController = new CardController();
            cardController.rechargeCard();
        } else {
            double blance = c.getCardBalance() - count;
            c.setCardBalance(blance);
            cards.set(i, c);
            addUserFile();
            System.out.println(cards);
            System.out.println("扣款成功，您的余额为:" + cards.get(i).getCardBalance());
        }

    }

    public void addUser() {
    }

    //将进行充值的id进行余额变化
    public void totalRechargeCard(String recId, double total) {
        //获取充值卡号所在容器的索引位置
        int index = getIndex(recId);
        for (int i = 0; i < cards.size(); i++) {
            //根据索引位置找到对象
            if (index == i) {
                Card card = cards.get(i);
                //将充值卡号的余额设置为充值后的金额
                card.setCardBalance(total);
            }
        }
    }
}